$(function() {
	var int = 0;
	$("#raty_star").click(function() {
		if ($("#default-demo3").raty('getScore') > 0 && $("#default-demo2").raty('getScore') > 0 && $("#default-demo1").raty('getScore') > 0) {
			layer.load();
			var this_btn = $(this);
			this_btn.attr("disabled", true);
			$.post('/user/evaluate/shop-comment', {
				service_desc_score: $("#default-demo1").raty('getScore'),
				send_desc_score: $("#default-demo2").raty('getScore'),
				logistics_speed_score: $("#default-demo3").raty('getScore'),
				shop_id: $(this).data("shop-id"),
				order_id: $(this).data("order-id")

			}, function(resule) {
				if (resule.code == 0) {
					$("#raty_stars").html(resule.data);
				}
				this_btn.attr("disabled", false);
				$.msg(resule.message);
				layer.closeAll("loading");

			}, 'json');
		} else {
			if ($("#default-demo1").raty('getScore') == undefined) {
				$.msg("请对店铺服务态度进行评价");
			} else if ($("#default-demo2").raty('getScore') == undefined) {
				$.msg("请对店铺发货速度进行评价");
			} else if ($("#default-demo3").raty('getScore') == undefined) {

				$.msg("请对店铺物流服务进行评价");
			}
		}
	})
	$("body").on("click", "input[class='btn append_button']", function() {
		var sku = $(this).data("sku-id");
		var img_num = $("#img_" + sku + " ul li").length;
		var text = $("#text_" + sku + "").val();
		var btn_this = $(this);
		if(text.length>400){
			$.msg("请将字数限制在400字以内");
			return;
		}
		if (text == "" && img_num == 1) {
			$("#submit_" + sku).css("display", "block");
		} else {
			btn_this.attr("disabled", true);
			layer.load();
			$.post('/user/evaluate/append', {
				comment_id: $(this).data("comment-id"),
				img_path: $("#imgpath_" + sku).val(),
				append_text: $("#text_" + sku).val(),
				sku_id: sku,
				order_id: $(this).data("order-id"),
				is_checked: btn_this.next().children("input").is(':checked')

			}, function(resule) {
				if (resule.code == 0) {
					$("#" + sku).html(resule.data);
				} else {

					btn_this.attr("disabled", false);
				}

				$.msg(resule.message);
				layer.closeAll("loading");
			}, 'json');
		}
	});
	$("body").on("click", ".act>input", function() {

		var sku = $(this).data("sku");
		var img_num = $("#img_" + sku + " ul li").length;
		var text = $("#text_" + sku + "").val();
		var num = $(this).parents("form").children(":first").children("label").children().length;
		var res_star = $(this).parents("form").children(":first").children("div").children("div").raty('getScore');
		var this_btn = $(this);
		if(text.length>400){
			$.msg("请将字数限制在400字以内");
			return;
		}

		if (res_star > 0 && (img_num > 1 || text != "")) {
			layer.load();
			this_btn.attr("disabled", true);
			$.post('/user/evaluate/posts', {
				score: res_star,
				sku_id: sku,
				order_id: $(this).data("order-id"),
				goods_id: $(this).data("goods-id"),
				img_path: $("#imgpath_" + sku).val(),
				shop_id: $(this).data("shop-id"),
				comment_text: text,
				is_checked: this_btn.next().children("input").is(':checked')

			}, function(resule) {
				if (resule.code == 0) {
					$("#" + sku).html(resule.data);
					$("#desc_core").html(resule.desc_core);

				} else {

					this_btn.attr("disabled", false);
				}
				$.msg(resule.message);
				layer.closeAll("loading");

			}, 'json');
		} else {
			if (res_star == undefined) {
				$("#star_" + sku).css("display", "block");
			} else {
				$("#star_" + sku).css("display", "none");
			}
			if (img_num == 1 && text == "") {
				$("#submit_" + sku).css("display", "block");

			} else {
				$("#submit_" + sku).css("display", "none");
			}
		}
	});
	$("body").on("click", ".reply", function() {
		var this_reply = $(this);

		var modal = $.modal({
			title: '回复卖家',
			content: $('#modal_box').html(),
		});
		modal.addButton({
			text: '发表回复',
			click: function() {
				var text = $(modal.container).find(".comment-cotnent").val();
				if (text == "") {
					$.msg("请输入回复内容");
				} else {
					$(this).attr("disabled", true);
					layer.load();
					var comment_id = $(modal.container).find("#comment_id").val();
					$.post('/user/evaluate/reply', {
						reply_text: text,
						comment: comment_id
					}, function(result) {
						if (result.code == 0) {
							this_reply.remove();
							modal.hide();
						} else {

							$(this).attr("disabled", false);
						}
						$.msg(result.message);
						layer.closeAll("loading");
					}, 'json');

				}
			}
		});
	});
})
